package com.ljt.handler;

import com.ljt.exceptions.BusinessException;
import com.ljt.exceptions.CaptchaException;
import com.ljt.response.Result;
import com.ljt.response.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public Result error(Exception e) {
        log.error(e.getMessage());
        return Result.error();
    }

    @ExceptionHandler(ArithmeticException.class)
    public Result error(ArithmeticException e) {
        log.error(e.getMessage());
        return Result.error()
                .code(ResultCode.ARITHMETIC_EXCEPTION.getCode())
                .message(ResultCode.ARITHMETIC_EXCEPTION.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    public Result error(BusinessException e) {
        log.error(e.getErrMsg());
        return Result.error()
                .code(e.getCode())
                .message(e.getErrMsg());
    }

    @ExceptionHandler(CaptchaException.class)
    public Result error(CaptchaException e) {
        log.error(e.getMessage());
        return Result.error()
                .code(ResultCode.CAPTCHA_VALIDATE_ERROR.getCode())
                .message(ResultCode.CAPTCHA_VALIDATE_ERROR.getMessage());
    }
}
